#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 6e3 + 5;

int n, m;
int r[N];
int nr[N], root;
vector<int> ch[N];
int dp[N][2];
void dfs(int u) {
  dp[u][1] = r[u];
  dp[u][0] = 0;
  for (int v : ch[u]) {
    dfs(v);
    dp[u][1] += dp[v][0];
    dp[u][0] += max(dp[v][1], dp[v][0]);
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) cin >> r[i];
  rep(i, 2, n) {
    int u, v;
    cin >> u >> v;
    ch[v].push_back(u);
    nr[u] = 1;
  }
  rep(i, 1, n) if (!nr[i]) {
    root = i;
    break;
  }
  dfs(root);
  cout << max(dp[root][0], dp[root][1]);
  return 0;
}